<template>
  <div class="login-wrap">
    <div class="ms-login" v-if="param.forgetPass">
      <div class="ms-title">教室管理系统</div>

      <el-tabs
        v-model="param.activeName"
        type="card"
        @tab-click="handleClick"
        :stretch="true"
        class="tab"
      >
        <el-tab-pane label="登录" name="first" class="first-tab"></el-tab-pane>
        <el-tab-pane label="注册" name="second" class="second-tab"></el-tab-pane>
      </el-tabs>
      <!-- 登录 -->
      <el-form
        :model="param"
        :rules="rules"
        ref="login"
        label-width="0px"
        class="ms-content"
        v-if="param.activeName === 'first'"
      >
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="请输入用户名">
            <el-button slot="prepend" icon="el-icon-s-custom"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="param.password"
            @keyup.enter.native="submitForm()"
          >
            <el-button slot="prepend" icon="el-icon-lock"></el-button>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm()">登录</el-button>
        </div>

        <p class="login-tips" @click="param.forgetPass=false">忘记密码</p>

        <!-- <el-button type="text" @click="open">点击打开 Message Box</el-button> -->
      </el-form>
      <!-- 注册 -->
      <el-form
        :model="param"
        :rules="rules"
        ref="login"
        label-width="0px"
        class="ms-content"
        v-if="param.activeName === 'second'"
      >
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="请输入登录名称">
            <el-button slot="prepend" icon="el-icon-s-custom"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="number">
          <el-input placeholder="请输入学号" v-model="param.number">
            <el-button slot="prepend" icon="el-icon-star-on"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="fullname">
          <el-input placeholder="请输入真实姓名" v-model="param.fullname">
            <el-button slot="prepend" icon="el-icon-info"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input type="password" placeholder="请输入密码" v-model="param.password">
            <el-button slot="prepend" icon="el-icon-lock"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="usertype">
          <el-radio-group v-model="param.usertype" icon="el-icon-s-custom">
            <el-radio label="管理员" value="0"></el-radio>
            <el-radio label="用户" value="1"></el-radio>
          </el-radio-group>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="registered()">注册</el-button>
        </div>
        <p class="login-tips">Tips : 欢迎注册</p>
      </el-form>
    </div>

    <div class="ms-login" v-else>
      <!-- 忘记密码 -->
      <div class="ms-title">忘记密码</div>

      <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="请输入用户名">
            <el-button slot="prepend" icon="el-icon-s-custom"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item prop="number">
          <el-input placeholder="请输入学号" v-model="param.number">
            <el-button slot="prepend" icon="el-icon-star-on"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item prop="newPassword">
          <el-input type="newPassword" placeholder="请输入新密码" v-model="param.newPassword">
            <el-button slot="prepend" icon="el-icon-lock"></el-button>
          </el-input>
        </el-form-item>

        <div class="login-btn">
          <el-button type="primary" @click="forgetPassword()">提交</el-button>
        </div>
        <p class="login-tips" @click="param.forgetPass=true">返回</p>
      </el-form>
    </div>
  </div>
</template>

<script>
import { log } from "util";
// import $axios from "../plugins/axios/index";
// import service from "../../../../vue-manageSystem/src/utils/request";
// import axios from "axios";
export default {
  data: function () {
    return {
      param: {
        username: "",
        password: "",

        activeName: "first",

        number: "",
        usertype: "",
        fullname: "",

        newPassword: "",
        forgetPass: true,
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],

        newPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
        ],

        number: [{ required: true, message: "请输入学号", trigger: "blur" }],
        usertype: [
          { required: true, message: "请选择用户类型", trigger: "blur" },
        ],
        fullname: [
          { required: true, message: "请输真实名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    handleClick(tab, event) {
      this.$refs.login.resetFields();
    },
    // 登录
    submitForm() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          this.$axios({
            url: "/api/login",
            method: "post",
            data: {
              username: this.param.username,
              password: this.param.password,
            },
          }).then((res) => {
            if (res._RejCode === "00000000") {
              this.$message.success("登录成功");
              sessionStorage.setItem("username", this.param.username);
              sessionStorage.setItem("userInfo", JSON.stringify(res));

              if (res.usertype == 0) {
                this.$router.push("/manageabout");
              } else {
                this.$router.push("/about");
              }
            }
          });
        } else {
          this.$message.error("请输入账号和密码");
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 注册
    registered() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          this.$axios
            .post("/api/register", {
              username: this.param.username,
              password: this.param.password,
              number: this.param.number,
              usertype: this.param.usertype=='用户'?'1':'0', // 用户类型，1用户，0管理员
              fullname: this.param.fullname,
            })
            .then((res) => {
              if (res._RejCode === "00000000") {
                this.$refs.login.resetFields();
                this.param.activeName = "first";
                this.$message({
                  message: "注册成功",
                  type: "success",
                });
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    forgetPassword() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          this.$axios
            .post("/api/changePassword", {
              username: this.param.username,
              newPassword: this.param.newPassword,
              number: this.param.number,
            })
            .then((res) => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  /* background-image: url("../assets/223143-1587997903e0f9.jpg"); */
  background-image: url("../assets/3.jpg");

  background-size: 100%;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}
.el-tabs--card > .el-tabs__header .el-tabs__nav {
  width: 100% !important;
}
.firse-tab {
  width: 50%;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 45%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
.forgetPass {
  width: 500px;
  height: 500px;
  z-index: 800;
  background-color: rgb(247, 242, 242, 0.5);
  position: absolute;
  left: 50%;
  top: 50%;

  margin: -190px 0 0 -175px;
}
.forgetPassword {
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
  /* z-index: 5; */
}
</style>